package com.socool.sinofaith.jsonparse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
/**
 * json解析
 * @author Administrator
 */
public class HttpJsonAdapter {

	private static HttpJsonAdapter instance = null;

	private HttpJsonAdapter() {
	}

	public static synchronized HttpJsonAdapter getInstance() {
		if (instance == null) {
			instance = new HttpJsonAdapter();
		}
		return instance;
	}

	public <T> T get(String str, Class<T> clazz) throws Exception {

		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
				.create();
		try {
			return (T) gson.fromJson(str, clazz);
		} catch (Exception e) {
			throw new Exception(e.getMessage() + "", e);
		}
	}
	
	/**
	 * 转化成gson字符串
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public String toJson(Object obj) throws Exception{
		
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
				.create();
		try {
			return gson.toJson(obj);
		} catch (Exception e) {
			throw new Exception(e.getMessage() + "", e);
		}
	}
}
